﻿using System;
using System.Security;
using System.Text;
using AchSegítő.extensions;

namespace AchSegítő.File
{
    /// <summary>
    /// Who is it from and what is it?
    /// </summary>
    public class BatchHeader
    {
        private int _batchNumber = 1;

        /// <summary>
        /// THIS IS THE FIRST POSITION FOR ALL RECORD FORMATS. CODE
        /// IS UNIQUE FOR EACH RECORD TYPE.THE COMPANY/BATCH HEADER RECORD USED RECORD TYPE CODE 5.
        /// </summary>
        public readonly RecordTypeCodes RecordTypeCode = RecordTypeCodes.CompanyBatchHeader;

        /// <summary>
        /// THE SERVICE CLASS CODE DEFINES THE TYPE OF ENTRIES CONTAINED IN THE BATCH.
        /// </summary>
        public ServiceClassCode ServiceClass = ServiceClassCode.Mixed_Debits_And_Credits;

        /// <summary>
        /// THIS FIELD IDENTIFIES THE COMPANY THAT HAS THE RELATIONSHIP WITH
        /// THE RECEIVERS OF THE ACH TRANSACTIONS. THE NAME MUST MATCH THE
        /// FROM FirstMerit BANK'S AGREEMENT. IN ACCORDANCE WITH FEDERAL 
        /// REGULATION E, MOST RECEIVING FINANCIAL  INSTITUTIONS WILL
        /// DISPLAY THIS FIELD ON THEIR CUSTOMER'S BANK STATEMENT.
        /// </summary>
        public String CompanyName { get; set; } = String.Empty;

// control name?

        /// <summary>
        /// RFERENCE INFORMATION FOR USE BY THE ORIGINATOR.
        /// </summary>
        public String CompanyDiscreationaryData { get; set; } = String.Empty;

        /// <summary>
        /// THIS FIELD IDENTIFIES THE ORIGINATOR OF THE TRANSACTION VIA THE
        /// ORIGINATOR'S FEDERAL TAX ID (IRS EIN). THIS FIELD BEGINS WITH THE
        /// NUMBER 1, FOLLOWED BY THE COMPANY'S 9-DIGIT TAX ID (WITHOUT A
        /// HYPHEN.) THE ID MUST MATCH THE ID LISTED ON THE "ACH EXHIBIT OR
        /// DOCUMENT B" FROM THE BANK'S AGREEMENT.
        /// </summary>
        public String CompanyId { get; set; } = String.Empty;

// ACH-EIN-Prefix + ControlEIN

        /// <summary>
        /// THIS FIELD DEFINES THE TYPE OF ACH ENTRIES CONTAINED IN THE BATCH.
        /// ENTER: PPD (PREARRANGED PAYMENTS AND DEPOSITS) FOR CONSUMER TRANSACTIONS DESTINED TO AN INDIVIDUAL or CCD
        /// (CASH CONCENTRATION OR DISBURSEMENT) FOR CORPORATE TRANSACTIONS.
        /// </summary> 
        public readonly string StandardEntryClassCode = File.StandardEntryClassCode.PrearrangedPaymentsDeposits;

        /// <summary>
        /// THIS FIELD IS USED BY THE ORIGINATOR TO PROVIDE A DESCRIPTION
        /// OF THE TRANSACTION FOR THE RECEIVER.FOR EXAMPLE, PAYROLL OR
        /// DIVIDEND, ETC.IN ACCORDANCE WITH REGULATION E, MOST RECEIVING
        /// BANKS WILL DISPLAY THIS FIELD ON THEIR BANK STATEMENT.
        /// </summary>
        public String EntryDescription { get; set; } = string.Empty;

        /// <summary>
        /// THIS FIELD IS USED BY THE ORIGINATOR TO PROVIDE A DESCRIPTIVE DATE
        /// FOR THE RECEIVER. THIS IS SOLELY FOR DESCRIPTIVE PURPOSES AND WILL
        /// NOT BE USED TO CALCULATE SETTLEMENT OR USED FOR POSTING PURPOSES.
        /// MANY RECEIVING FINANCIAL INSTITUTIONS WILL DISPLAY THIS FIELD ON
        /// THE CONSUMER'S BANK STATEMENT.
        /// </summary>
        public DateTime CompanyDescriptiveDate { get; set; } = DateTime.Today;

// CheckDate

        /// <summary>
        /// THIS REPRESENTS THE DATE ON WHICH THE ORIGINATOR INTENDS A BATCH OF ENTRIES TO BE SETTLED.
        /// </summary>
        public DateTime EffectiveEntryDate { get; set; } = DateTime.Today;

        public String SettlementDate { get; set; } = String.Empty;

        public readonly OriginatorStatusCodes OriginatorStatusCode = OriginatorStatusCodes.NachaBound;

        /// <summary>
        /// ENTER THE FIRST 8 DIGITS OF YOUR BANK ABA OR TRANSIT ROUTING NUMBER.
        /// </summary>
        public String OriginatingDFIIdentification { get; set; } = String.Empty;

// ACHBankRoutingNumber

        /// <summary>
        /// USED BY THE ORIGINATOR TO ASSIGN A NUMBER
        /// IN ASCENDING SEQUENCE TO EACH BATCH IN THE FILE.
        /// </summary>
        public int BatchNumber
        {
            get { return _batchNumber; }
            set { _batchNumber = value; }
        }

        public override string ToString()
        {
            var sb = new StringBuilder(ACH.CHARACTERS_PER_LINE);

            sb.Append((int)RecordTypeCode);

            sb.Append((int)ServiceClass);

            sb.Append(CompanyName.TrimAndPadRight(16));

            sb.Append(CompanyDiscreationaryData.TrimAndPadRight(20));

            sb.Append(CompanyId.TrimAndPadRight(10));

            sb.Append(StandardEntryClassCode.TrimAndPadRight(3));

            sb.Append(EntryDescription.TrimAndPadRight(10));

            sb.Append(CompanyDescriptiveDate.ToString("yyMMdd"));

            sb.Append(EffectiveEntryDate.ToString("yyMMdd"));

            sb.Append(SettlementDate.TrimAndPadLeft(3));

            sb.Append((int)OriginatorStatusCode);

            sb.Append(OriginatingDFIIdentification.TrimAndPadRight(8));

            sb.Append(BatchNumber.ToString().TrimAndPadLeft(7, '0'));

            sb.AppendLine();

            return sb.ToString();
        }

    }
}